package com.app.hmst.main;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.app.hmst.model.AssessorVO;
import com.app.hmst.model.HomestayInfoVO;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.roscopeco.ormdroid.Entity;

@SuppressLint({ "NewApi", "ValidFragment" }) public class GeneralInfoForAssessor extends Fragment{
	private View v;
	private SlidingMenu _sm;
	private ImageView menu_icon,calendar_icon;
	private Button saveGenInfo,edit_general_assessor;
	private EditText title,assessorDate,assessorOrg;
	private HomestayInfoVO _homeStayInfo;
	private int mYear, mMonth, mDay, mHour, mMinute;
	private RelativeLayout gotoGeneralPage,gotoEvaluatePage,gotoSuggestionPage;
	public GeneralInfoForAssessor(SlidingMenu sm,HomestayInfoVO result){
		this._sm = sm;
		this._homeStayInfo = result;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		v = inflater.inflate(R.layout.fragment_generalinfo_page_for_assessor,container, false);
		
	
		saveGenInfo = (Button) v.findViewById(R.id.imageView13);
		edit_general_assessor = (Button) v.findViewById(R.id.edit_general_assessor);
		title = (EditText) v.findViewById(R.id.editText1);
		
		assessorOrg = (EditText) v.findViewById(R.id.editText4);
		gotoGeneralPage = (RelativeLayout) v.findViewById(R.id.relativeLayout2);
		gotoEvaluatePage = (RelativeLayout) v.findViewById(R.id.relativeLayout3);
		gotoSuggestionPage = (RelativeLayout) v.findViewById(R.id.relativeLayout4);
		assessorDate = (EditText) v.findViewById(R.id.editText6);
		
		
		calendar_icon = (ImageView) v.findViewById(R.id.calendar_icon);
		
		gotoGeneralPage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new GeneralInfoFragment(_sm, _homeStayInfo));
			}
		});
		gotoEvaluatePage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new EvaluateFormFragment(_sm, _homeStayInfo,true));
			}
		});
		gotoSuggestionPage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new FormNotePageFragment(_sm,_homeStayInfo));
			}
		});
		final AssessorVO assessorVO = Entity.query(AssessorVO.class).whereId(_homeStayInfo.id).execute();
		if(assessorVO!=null){
			title.setText(assessorVO.assessor_name);
			assessorOrg.setText(assessorVO.assessor_org);
			assessorDate.setText(assessorVO.assessor_date);
			title.setEnabled(false);
			assessorOrg.setEnabled(false);
			assessorDate.setEnabled(false);
			edit_general_assessor.setVisibility(View.VISIBLE);
		}else{
			saveGenInfo.setVisibility(View.VISIBLE);
			edit_general_assessor.setVisibility(View.INVISIBLE);
		}
		//work Around 
		edit_general_assessor.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				title.setEnabled(true);
				assessorOrg.setEnabled(true);
				assessorDate.setEnabled(true);
				saveGenInfo.setVisibility(View.VISIBLE);
				edit_general_assessor.setVisibility(View.INVISIBLE);
			}
		});
		saveGenInfo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(assessorVO==null){
					String assessorName = title.getText().toString();
					String assessorOrgStr = assessorOrg.getText().toString();
					String assessorDateStr = assessorDate.getText().toString();
					if(Utils.isNotEmpty(assessorName) && Utils.isNotEmpty(assessorOrgStr) && Utils.isNotEmpty(assessorDateStr)){
						AssessorVO ass = new AssessorVO();
						ass.id = _homeStayInfo.id;
						ass.assessor_date = assessorDateStr;
						ass.assessor_name = assessorName;
						ass.assessor_org = assessorOrgStr;
						ass.homeId = _homeStayInfo.id+"";
						ass.save();							
						switchFragment(new GeneralInfoFragment(_sm, _homeStayInfo));
					}else{
						if(!Utils.isNotEmpty(assessorName)){
							title.setError(getResources().getString(R.string.error_assessor));
						}
						if(!Utils.isNotEmpty(assessorOrgStr)){
							assessorOrg.setError(getResources().getString(R.string.error_assessor));
						}
						if(!Utils.isNotEmpty(assessorDateStr)){
							assessorDate.setError(getResources().getString(R.string.error_assessor));
						}
					}
				}else{
				AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
		        builder1.setMessage(getString(R.string.alreadysave_message));
		        builder1.setCancelable(true);
		        builder1.setPositiveButton(getString(R.string.homestay_sync_ok),new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		                String assessorName = title.getText().toString();
						String assessorOrgStr = assessorOrg.getText().toString();
						String assessorDateStr = assessorDate.getText().toString();
						if(Utils.isNotEmpty(assessorName) && Utils.isNotEmpty(assessorOrgStr) && Utils.isNotEmpty(assessorDateStr)){
							AssessorVO ass = new AssessorVO();
							ass.id = _homeStayInfo.id;
							ass.assessor_date = assessorDateStr;
							ass.assessor_name = assessorName;
							ass.assessor_org = assessorOrgStr;
							ass.homeId = _homeStayInfo.id+"";
							ass.save();							
							switchFragment(new GeneralInfoFragment(_sm, _homeStayInfo));
						}else{
							if(!Utils.isNotEmpty(assessorName)){
								title.setError(getResources().getString(R.string.error_assessor));
							}
							if(!Utils.isNotEmpty(assessorOrgStr)){
								assessorOrg.setError(getResources().getString(R.string.error_assessor));
							}
							if(!Utils.isNotEmpty(assessorDateStr)){
								assessorDate.setError(getResources().getString(R.string.error_assessor));
							}
						}
		            }
		        });
		        AlertDialog alert11 = builder1.create();
		        alert11.show();
				}
			}
		});
		assessorDate.setFocusableInTouchMode(false);
		assessorDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final Calendar c = Calendar.getInstance();
	            mYear = c.get(Calendar.YEAR);
	            mMonth = c.get(Calendar.MONTH);
	            mDay = c.get(Calendar.DAY_OF_MONTH);
	 
	            // Launch Date Picker Dialog
	            DatePickerDialog dpd = new DatePickerDialog(getActivity(),
	                    new DatePickerDialog.OnDateSetListener() {
	 
	                        @Override
	                        public void onDateSet(DatePicker view, int year,
	                                int monthOfYear, int dayOfMonth) {
	                            // Display Selected date in textbox
	                        	assessorDate.setText(dayOfMonth + "-"+ (monthOfYear + 1) + "-" + year);
	 
	                        }
	                    }, mYear, mMonth, mDay);
	            dpd.show();
			}
		});	
		
		menu_icon = (ImageView) v.findViewById(R.id.imageView6);
		menu_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_sm.toggle();
			}
		});
		new Utils(getActivity()).onBackPressed(v, new StartEvaluateFragment(_sm),null);
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	
	public class InitAndLoadData extends AsyncTask<String, Void, List<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub			
			List<String> list = new ArrayList<String>();
			list.add("000");
			return list;
		}
		
		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			if (result != null) {
				
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
}
